Programming for Finance p. 11

by: Chicolaj, 8 years ago

Last edited: 8 years ago

Hi,

I'm following the Programming for Finance tutorial using Python 2.7.10, and I've come across the following issue that I cannot get my head around:

<pre class='prettyprint lang-py'>
('Data spread:', Counter({'1': 2098, '-1': 1830, '0': 349}))
Traceback (most recent call last):
  File "finance1.py", line 169, in <module>
    extract_featuresets('AAPL')
  File "finance1.py", line 160, in extract_featuresets
    df_vals = df[[ticker for ticker in tickers]].pct_change()
  File "C:Python27libsite-packagespandascoreframe.py", line 2053, in __getitem__
    return self._getitem_array(key)
  File "C:Python27libsite-packagespandascoreframe.py", line 2097, in _getitem_array
    indexer = self.ix._convert_to_indexer(key, axis=1)
  File "C:Python27libsite-packagespandascoreindexing.py", line 1230, in _convert_to_indexer
    raise KeyError('%s not in index' % objarr[mask])
KeyError: "['P'] not in index"
</pre>

I also tried with 'GOOG' in which case I got back:

<pre class='prettyprint lang-py'>
KeyError: "['G' 'G'] not in index"
</pre>

So the vowels are disappearing?!

My code for the specific area is a complete ripoff:
<pre class='prettyprint lang-py'>
vals = df['{}_target'.format(ticker)].values.tolist()
str_vals = [str(i) for i in vals]
print('Data spread:', Counter(str_vals))

df.fillna(0, inplace=True)
df = df.replace([np.inf, -np.inf], np.nan)
df.dropna(inplace=True)

df_vals = df[[ticker for ticker in tickers]].pct_change()
df_vals = df_vals.replace([np.inf, -np.inf], 0)
df_vals.fillna(0, inplace=True)

X = df_vals.values
y = df['{}_target'.format(ticker)].values

return X, y, df

extract_featuresets('GOOG')
</pre>

I've been trying to Google my way to a solution, but I'm stuck.

I would really appreciate any and pointers.

Thanks!



You must be logged in to post. Please login or register an account.



You're clearly iterating over the string, rather than a list.

I am not looking at your full code, nor the code from the series, but the output tells me it's iterating over whatever you pass, like in a for loop.

extract_featuresets('GOOG')

You're getting errors on specific letters from the ticker you passed, but not all. This is because, in the case of AAPL, "A" is a valid ticker. "P" is not. "L" is.

To fix it, you probably can just do:

extract_featuresets(['GOOG'])

-Harrison 8 years ago

You must be logged in to post. Please login or register an account.


Thank you for the response! I'll give it a go when I get home from work.

-Chicolaj 8 years ago

You must be logged in to post. Please login or register an account.